/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

define(['adminapp', 'core/datatable', 'bootstrap-datepicker', 'bootstrap-datepicker-zh'], function ($app) {
    $app.navactive('service_ticket');

    var added_tpl = Handlebars.compile($("#added_template").html());
    var action_tpl = Handlebars.compile($("#action_template").html());
    var pricute_tpl = Handlebars.compile($("#pricute_template").html());
    var color_tpl = Handlebars.compile($("#color_temple").html());
    var errortip_tpl = Handlebars.compile($("#errortip_temple").html());
    var alertcontent = $("#alertcontent_template").html();

    var _grid = new Datatable();
    _grid.init({
        src      : $("#datatable"),
        onSuccess: function (grid) {
            jQuery(".fancybox-button").fancybox({
                groupAttr: 'data-rel',
                prevEffect: 'none',
                nextEffect: 'none',
                closeBtn: true,
                helpers: {
                    title: {
                        type: 'inside'
                    }
                }
            });
            $('.date-picker').datepicker({
                language      : 'zh-CN',
                rtl: App.isRTL(),
                autoclose: true
            });
        },
        onError  : function (grid) {
        },
        dataTable: {
            "bServerSide"   : true,
            "sAjaxSource"   : g.ctx + 'customer/ticket/list',
            "aaSorting"     : [
                [ 1, "asc" ]
            ],
            'aoColumns'   : [
                $app.checkall,
                $app.autono,
                {'mDataProp': 'card_no' },
                {'mDataProp': 'phone' },
               /* {'mDataProp': 'wx_no' },*/
                {'mDataProp': 'pricute' },
                {'mDataProp': 'upload_time' },
                {'mDataProp': 'ticketNo' },
                {'mDataProp': 'added' },
                {'mDataProp': 'add_time' },
                {'mDataProp': 'integral' },
                {'mDataProp': 'addIntegral' }
            ],
            'aoColumnDefs': [
                {
                    'aTargets': [4],
                    'mData'   : null,
                    'mRender' : function (data, type, full) {
                        return pricute_tpl(full);
                    }
                },
                {
                    'aTargets': [7],
                    'mData'   : null,
                    'mRender' : function (data, type, full) {
                        return added_tpl(full);
                    }
                },
                {
                    'aTargets': [9],
                    'mData'   : null,
                    'mRender' : function (data, type, full) {
                        var context = {classname : 'label-primary', value: full['integral']};
                        return color_tpl(context);
                    }
                },
                {
                    'aTargets': [10],
                    'mData'   : null,
                    'mRender' : function (data, type, full) {
                        var context = {classname : 'label-warning', value: full['addIntegral']};
                        return color_tpl(context);
                    }
                },
                {
                    'aTargets': [11],
                    'mData'   : null,
                    'mRender' : function (data, type, full) {
                        return action_tpl(full);
                    }
                }
            ],
            'fnRowCallback'  : function (nRow, aData, iDisplayIndex) {
                var oSettings = _grid.getDataTable().fnSettings();
                $("td:eq(1)", nRow).html(oSettings._iDisplayStart+iDisplayIndex +1);
                return nRow;
            }
        }
    });
    var re = /^-?\d+\.?\d{0,1}$/;
    $("#datatable").delegate('a[name="add_intgral"]', 'click', function() {
        var memberId = $(this).attr('data-member');
        var ticketId = $(this).attr('data-ticket');
        if (ticketId && memberId) {
            bootbox.dialog({
                message: alertcontent,
                title: "小票积分",
                buttons: {
                    success: {
                        label: "确定",
                        className: "btn-success",
                        callback: function() {
                            var ticketNoEl = $('.modal-body').find('input[name="ticketNo"]');
                            var integralEl = $('.modal-body').find('input[name="integral"]');
                            var ticketNo = ticketNoEl.val();
                            var integral = integralEl.val();
                            if (!ticketNo) {
                                ticketNoEl.parent().find('.error').remove();
                                ticketNoEl.parent().append(errortip_tpl({tip : '请输入小票号'}));
                                ticketNoEl.css('border-color', '#b94a48');
                                return false;
                            }
                            if (!integral) {
                                integralEl.parent().find('.error').remove();
                                integralEl.parent().append(errortip_tpl({tip : '请输入积分'}));
                                integralEl.css('border-color', '#b94a48');
                                return false;
                            }
                            if (!re.test(integral)) {
                                integralEl.parent().find('.error').remove();
                                integralEl.parent().append(errortip_tpl({tip : '请输入合法数值'}));
                                integralEl.css('border-color', '#b94a48');
                                return false;
                            }
                            var b = false;
                            $.ajax({
                                url : g.ctx + 'customer/ticket/exchange',
                                data: {ticketId : ticketId, memberId : memberId, integral : integral, ticketNo : ticketNo},
                                async: false,
                                success: function(data) {
                                    if (data.status == 'OK') {
                                        //window.location.reload();
                                        _grid.getTable().fnDraw();
                                        b = true;
                                    } else {
                                        ticketNoEl.parent().find('.error').remove();
                                        ticketNoEl.parent().append(errortip_tpl({tip : data.message}));
                                        ticketNoEl.css('border-color', '#b94a48');
                                        b = false;
                                    }
                                }
                            });
                            return b;
                        }
                    },
                    danger: {
                        label: "取消",
                        className: "btn-danger",
                        callback: function() {
                            //Example.show("uh oh, look out!");
                        }
                    }
                }
            });
        }
    });
    $(document).delegate('.bootbox-ticket-input', 'focus', function() {
        $(this).css('border-color', '#e5e5e5');
        $(this).parent().find('.error').remove();
    });
    return {};
});